package com.example.kotin.myfragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import java.util.List;

public class RecyclerMultiAdapter extends RecyclerView.Adapter {
    //定义三种常量  表示三种条目类型
    private static final int TYPE_PULL_IMAGE = 0;
    private static final int TYPE_RIGHT_IMAGE = 1;
    private static final int TYPE_THREE_IMAGE = 2;
    private List<MoreTypeBean> mData;
    private ItemClickListener mItemClick;

    public RecyclerMultiAdapter(List<MoreTypeBean> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建不同的 ViewHolder
        View view;
        //根据viewtype来创建条目

        if (viewType == TYPE_PULL_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_pull_img, null);
            return new PullImageHolder(view);
        } else if (viewType == TYPE_RIGHT_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_right_img, null);
            return new RightImageHolder(view);
        } else {
            view = View.inflate(parent.getContext(), R.layout.item_three_img, null);
            return new ThreeImageHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClick.onClickListener(holder.itemView, position);
            }
        });
    }

    public void setOItemClick(ItemClickListener itemClick) {

        mItemClick = itemClick;
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    //根据条件返回条目的类型
    @Override
    public int getItemViewType(int position) {

        MoreTypeBean moreTypeBean = mData.get(position);
        if (moreTypeBean.type == 0) {
            return TYPE_PULL_IMAGE;
        } else if (moreTypeBean.type == 1) {
            return TYPE_RIGHT_IMAGE;
        } else {
            return TYPE_THREE_IMAGE;
        }
    }

    /**
     * 创建三种ViewHolder
     */
    private static class PullImageHolder extends RecyclerView.ViewHolder {

        private final ImageView mPullImage;

        private PullImageHolder(View view) {
            super(view);
            mPullImage = view.findViewById(R.id.pull_image);
            ScaleAnimation scaleXY = AnimationUtils.scaleXY();
            mPullImage.startAnimation(scaleXY);
            scaleXY.start();
        }
    }

    private static class RightImageHolder extends RecyclerView.ViewHolder {

        private final ImageView mRight;

        private RightImageHolder(View view) {
            super(view);
            mRight = view.findViewById(R.id.right_image);
            ScaleAnimation scaleXY = AnimationUtils.scaleXY();
            mRight.startAnimation(scaleXY);
            scaleXY.start();
        }
    }

    private static class ThreeImageHolder extends RecyclerView.ViewHolder {

        private final ImageView mOne;
        private final ImageView mTwo;
        private final ImageView mThree;

        private ThreeImageHolder(View view) {
            super(view);
            mOne = view.findViewById(R.id.three_one_image);
            mTwo = view.findViewById(R.id.three_two_image);
            mThree = view.findViewById(R.id.three_three_image);
            ScaleAnimation scaleXY = AnimationUtils.scaleXY();
            mOne.startAnimation(scaleXY);
            mTwo.startAnimation(scaleXY);
            mThree.startAnimation(scaleXY);
            scaleXY.start();

        }
    }
}
